package civicinity.ist.psu.edu.share;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.widget.Toast;
import civicinity.ist.psu.edu.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class CivicinityMapOverlay extends BalloonItemizedOverlay<CivicinityOverlayItem>{
	private Context mContext;
	private ArrayList<ShareItem> mShareArr = new ArrayList<ShareItem>();
	private ArrayList<CivicinityOverlayItem> mSItem = new ArrayList<CivicinityOverlayItem>();
	private Drawable mMarker;
	private MapView mMap = null;
	private CivicinityOverlayItem speciesItem;
	private int mIndex;
	
	public CivicinityMapOverlay(MapView mapView, Drawable defaultMarker, ArrayList<ShareItem> arr) {
		super(boundCenterBottom(defaultMarker), mapView);
		
		mContext = mapView.getContext();
		mShareArr = arr;

		// read data from the table
		for(int i = 0 ; i < mShareArr.size() ; i++) {
			GeoPoint geoPoint = getPoint(mShareArr.get(i).getEventLatitude(), mShareArr.get(i).getEventLongitude());

			mSItem.add(new CivicinityOverlayItem(geoPoint, 
					mShareArr.get(i).getEventID(), 
					mShareArr.get(i).getEventDescription(),
					mShareArr.get(i).getEventPhotoUrl(),
					mShareArr.get(i).getEventCommentCnt(),
					mShareArr.get(i).getEventLike(),
					getMarker(R.drawable.marker), 
					mMarker));
		}
	
		populate();
	}
	
	private Drawable getMarker(int resource) {
		Drawable marker = mContext.getResources().getDrawable(resource);
		marker.setBounds(-marker.getIntrinsicWidth() / 2, marker.getIntrinsicHeight(), marker.getIntrinsicWidth() / 2, 0);
		boundCenterBottom(marker);
		return(marker);
	}

	@Override
	protected CivicinityOverlayItem createItem(int i) {
		return(mSItem.get(i));
	}

	private Bitmap LoadImageFromWebOperation(String url) {
		try {
			URL imageURL = new URL(url);
			HttpURLConnection conn = (HttpURLConnection)imageURL.openConnection();
			conn.setDoInput(true);
			conn.connect();
			
			InputStream is = conn.getInputStream();
			Bitmap bitmap = BitmapFactory.decodeStream(is);
			
			return bitmap; 
			
		}
		catch(Exception e) {
			return null;
		}
	}
	
	private GeoPoint getPoint(double lat, double lon) {
		return(new GeoPoint((int)(lat*1000000.0), (int)(lon*1000000.0)));
	}

	
	/**
	 * Handle when the user press the balloon button.
	 */
	@Override
	protected boolean onBalloonTap(int index) {
		// Observed species are from PlantList and Shared Plant
		
		mIndex = index;

		// add something later
		showDialog(index);
		
		hideBalloon();
		
		return true;
	}
	
	
	
	private void showDialog(int index) {
		
		//TODO : move to the detail page?
		Intent intent = new Intent(mContext, CivicinityShareInfo.class);
		intent.putExtra("event_id", mShareArr.get(index).getEventID());
		
		mContext.startActivity(intent);
	}

	
	@Override
	public int size() {
		return(mSItem.size());
	}
	
	
	void toggleHeart() {
		CivicinityOverlayItem focus = getFocus();
		
		if (focus!=null) {
			focus.toggleHeart();
		}
		
		mMap.invalidate();
	}
}
